package com.example.ymm.modules1.service.impl.ms;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.ms.VckStock;
import com.example.ymm.modules1.entity.ms.ViewBaogonCailiao;
import com.example.ymm.modules1.entity.my.ms.PmcOrder;
import com.example.ymm.modules1.entity.my.ms.PmcQueLiao;
import com.example.ymm.modules1.mapper.my.ms.PmcOrderMapper;
import com.example.ymm.modules1.query.ms.PmcQueryCriteria;
import com.example.ymm.modules1.query.ms.QueLiaosQueryCriteria;
import com.example.ymm.modules1.service.ms.PmcOrderService;
import com.example.ymm.modules1.service.ms.PmcQueLiaoService;
import com.example.ymm.modules1.service.ms.VckStockService;
import com.example.ymm.util.DateKit;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalTime;
import java.util.*;

@Service
@Slf4j
@DS("slave2")
@DSTransactional
public class PmcOrderServiceImpl extends BaseServiceImpl<PmcOrderMapper, PmcOrder> implements PmcOrderService {


    @Autowired
    VckStockService vckStockService;

    @Autowired
    PmcQueLiaoService pmcQueLiaoService;

    @Override
    public void autoSync(String buMenName) {
      //  System.out.println("---isBetween8And19()"+isBetween8And19());
        if(DateKit.isBetween8And19()){
//            System.out.println("--执行定时器逻辑");
            int page=1;
            int pageSize=500;
//            TableList<ViewBaogonCailiao> tableList = operatorNextPage(page, pageSize);
//            long totalResult = tableList.getTotalPage();
//            for (int i = 0; i < totalResult; i++) {
//                page+=1;
//                operatorNextPage(page, pageSize);
//            }
            operatorNextPage(page, pageSize,buMenName);
//            if(tableList.getTotalPage()>1){
//                operatorNextPage(2, pageSize);
//            }
        }

    }


    @Override
    public PmcOrder getByBuMenNameAndBiaoShiHao(String buMenName, String biaoShiHao) {
        LambdaQueryWrapper<PmcOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmcOrder::getBuMenName, buMenName);
        queryWrapper.eq(PmcOrder::getBiaoShiHao, biaoShiHao);
        List<PmcOrder> pmcOrders = baseMapper.selectList(queryWrapper);
        if(pmcOrders.size()>0){
            return pmcOrders.get(0);
        }
        return null;
    }

    /**
     * 同步 未完成，缺料
     * @param page
     * @param pageSize
     * @param buMenName
     * @return
     */
    public TableList<ViewBaogonCailiao> operatorNextPage(int page, int pageSize, String buMenName){
        PmcQueryCriteria criteria = new PmcQueryCriteria();
        criteria.setIsQiTao(1);
        criteria.setBuMenName(buMenName);
        YmPageDto ymPage=new YmPageDto();
        ymPage.setPage(page);
        Date date = DateKit.dateAdd(3, new Date(), -6);
        String s = DateKit.dateFormat(date, "yyyy-MM-01");
        criteria.setStartTime(s);
        ymPage.setPageSize(pageSize);
        // criteria.setBiaoShiHao("SJJ2503004001");
        TableList<ViewBaogonCailiao> tableList = vckStockService.pmcTableList(criteria, ymPage);
        List<ViewBaogonCailiao> list = tableList.getList();
        List<String> biaoShiHaoIn=new ArrayList<String>();
        list.forEach(item->{
            biaoShiHaoIn.add(item.getBiaoShiHao());
        });
        if(UtilValidate.isNotEmpty(biaoShiHaoIn)){
            PmcQueryCriteria pmcQueryCriteria=new PmcQueryCriteria();
            pmcQueryCriteria.setBiaoShiHaoIn(biaoShiHaoIn);
            pmcQueryCriteria.setBuMenName(buMenName);
            List<ViewBaogonCailiao> baogonCailiaos = vckStockService.baogongQueryList(pmcQueryCriteria);
            list.forEach(item1->{
                baogonCailiaos.forEach(item2->{
                    if(item2.getBiaoShiHao().equals(item1.getBiaoShiHao())&&UtilValidate.isNotEmpty(item2.getBaoZhuanNum())){
//                            item.chengpingNum=item2.chengpingNum;//成品组装
//                            item.biaoTouNum=item2.biaoTouNum;//表头组装
//                            item.chuanDaiNum=item2.chuanDaiNum;//穿带
//                            item.baoZhuanNum=item2.baoZhuanNum;//包装
                        BigDecimal totalUnDo=item1.getXiaoShouShuLiang().subtract(item2.getBaoZhuanNum());
                        int i = totalUnDo.compareTo(BigDecimal.ZERO);
                        if(i<=0){//
                            System.out.println("--同步订单标识号："+item2.getBiaoShiHao());
                            try {

                                PmcOrder pmcOrder = new PmcOrder();
                                pmcOrder.setStatus(1);
                                pmcOrder.setBiaoShiHao(item2.getBiaoShiHao());
                                pmcOrder.setBuMenName(buMenName);
                                pmcOrder.setId(item2.getBiaoShiHao()+buMenName);
                                saveOrUpdate(pmcOrder);

                            }catch (Exception e){
                                log.error("pmc同步已完成订单异常:"+e.getMessage());
                            }
                        }

                        //同步缺料情况
                    }
                });
            });
            QueLiaosQueryCriteria c2=new QueLiaosQueryCriteria();
            c2.setBuMenName(buMenName);
            c2.setBiaoShiHaoIn(biaoShiHaoIn);
            //处理缺料
            Map<String, String> queLiaos = vckStockService.getQueLiaos(c2);
            list.forEach(item1->{
//                PmcQueLiao pmcQueLiao = pmcQueLiaoService.getByBuMenNameAndBiaoShiHao(buMenName, item1.getBiaoShiHao());
//                if(UtilValidate.isEmpty(pmcQueLiao)){
//
//                }
                PmcQueLiao pmcQueLiao = new PmcQueLiao();
                pmcQueLiao.setBiaoShiHao(item1.getBiaoShiHao());
                pmcQueLiao.setBuMenName(buMenName);
                pmcQueLiao.setId(item1.getBiaoShiHao()+buMenName);
                Set<Map.Entry<String, String>> entries = queLiaos.entrySet();
                for (Map.Entry<String, String> entry : entries) {
                    String k = entry.getKey();
                    String v = entry.getValue();
                    if(k.equals(item1.getBiaoShiHao())){
                        pmcQueLiao.setIzQiTao(UtilValidate.isNotEmpty(v)?0:1);
                        pmcQueLiao.setQueLiao(v);
                        pmcQueLiao.setBiaoShiHao(k);
                    }
                }
                if(UtilValidate.isNotEmpty(pmcQueLiao.getQueLiao())){
                    pmcQueLiao.setIzQiTao(0);
                }else{
                    pmcQueLiao.setIzQiTao(1);
                }
                try {
                    pmcQueLiaoService.saveOrUpdate(pmcQueLiao);
                }catch (Exception e){
                    log.error("pmc同步 缺料异常:"+e.getMessage());
                }
            });
        }
        return tableList;
    }


}
